

##########################################################
# ATEs (using dichotomous DVs)
##########################################################

#convert favor variable to 1s and 0s

mturk$favor2 <- mturk$favor
mturk$favor2[mturk$favor == -1] <- 0 

# Study 1

table(subset(mturk, issue=="FOR")$group, subset(mturk, issue=="FOR")$favor2)
table(subset(mturk, issue=="JNK")$group, subset(mturk, issue=="JNK")$favor2)
table(subset(mturk, issue=="NET")$group, subset(mturk, issue=="NET")$favor2)

# Study 2

table(subset(mturk, issue=="IMM")$group, subset(mturk, issue=="IMM")$favor2)
table(subset(mturk, issue=="SOC")$group, subset(mturk, issue=="SOC")$favor2)
table(subset(mturk, issue=="VAC")$group, subset(mturk, issue=="VAC")$favor2)

# test significance using the following formula:

t.test(subset(mturk, issue=="FOR" & group=="control1")$favor2, subset(mturk, issue=="FOR" & group=="control2")$favor2)
t.test(subset(mturk, issue=="FOR" & group=="control1")$favor2, subset(mturk, issue=="FOR" & group=="treatment1")$favor2)
t.test(subset(mturk, issue=="FOR" & group=="control1")$favor2, subset(mturk, issue=="FOR" & group=="treatment2")$favor2)
t.test(subset(mturk, issue=="FOR" & group=="control2")$favor2, subset(mturk, issue=="FOR" & group=="treatment1")$favor2)
t.test(subset(mturk, issue=="FOR" & group=="control2")$favor2, subset(mturk, issue=="FOR" & group=="treatment2")$favor2)
t.test(subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$favor2, subset(mturk, issue=="FOR" & group=="treatment2")$favor2)


t.test(subset(mturk, issue=="JNK" & group=="control1")$favor2, subset(mturk, issue=="JNK" & group=="control2")$favor2)
t.test(subset(mturk, issue=="JNK" & group=="control1")$favor2, subset(mturk, issue=="JNK" & group=="treatment1")$favor2)
t.test(subset(mturk, issue=="JNK" & group=="control1")$favor2, subset(mturk, issue=="JNK" & group=="treatment2")$favor2)
t.test(subset(mturk, issue=="JNK" & group=="control2")$favor2, subset(mturk, issue=="JNK" & group=="treatment1")$favor2)
t.test(subset(mturk, issue=="JNK" & group=="control2")$favor2, subset(mturk, issue=="JNK" & group=="treatment2")$favor2)
t.test(subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$favor2, subset(mturk, issue=="JNK" & group=="treatment2")$favor2)

t.test(subset(mturk, issue=="NET" & group=="control1")$favor2, subset(mturk, issue=="NET" & group=="control2")$favor2)
t.test(subset(mturk, issue=="NET" & group=="control1")$favor2, subset(mturk, issue=="NET" & group=="treatment1")$favor2)
t.test(subset(mturk, issue=="NET" & group=="control1")$favor2, subset(mturk, issue=="NET" & group=="treatment2")$favor2)
t.test(subset(mturk, issue=="NET" & group=="control2")$favor2, subset(mturk, issue=="NET" & group=="treatment1")$favor2)
t.test(subset(mturk, issue=="NET" & group=="control2")$favor2, subset(mturk, issue=="NET" & group=="treatment2")$favor2)
t.test(subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$favor2, subset(mturk, issue=="NET" & group=="treatment2")$favor2)

t.test(subset(mturk, issue=="IMM" & group=="control")$favor2, subset(mturk, issue=="IMM" & group=="treatment")$favor2)
t.test(subset(mturk, issue=="SOC" & group=="control")$favor2, subset(mturk, issue=="SOC" & group=="treatment")$favor2)
t.test(subset(mturk, issue=="VAC" & group=="control")$favor2, subset(mturk, issue=="VAC" & group=="treatment")$favor2)


##########################################################
# ATEs (using composite DVs)
##########################################################

t.test(subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$composite, subset(mturk, issue=="FOR" & group=="treatment1")$composite)
t.test(subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$composite, subset(mturk, issue=="JNK" & group=="treatment1")$composite)
t.test(subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$composite, subset(mturk, issue=="NET" & group=="treatment1")$composite)

t.test(subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$composite, subset(mturk, issue=="FOR" & group=="treatment2")$composite)
t.test(subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$composite, subset(mturk, issue=="JNK" & group=="treatment2")$composite)
t.test(subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$composite, subset(mturk, issue=="NET" & group=="treatment2")$composite)

t.test(subset(mturk, issue=="IMM" & group=="control")$composite, subset(mturk, issue=="IMM" & group=="treatment")$composite)
t.test(subset(mturk, issue=="SOC" & group=="control")$composite, subset(mturk, issue=="SOC" & group=="treatment")$composite)
t.test(subset(mturk, issue=="VAC" & group=="control")$composite, subset(mturk, issue=="VAC" & group=="treatment")$composite)


# get confidence intervals

CIs <- rbind(
t.test(subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="FOR" & group=="treatment1")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="FOR" & group=="treatment2")$composite)$conf.int[1:2],
NA,
t.test(subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="JNK" & group=="treatment1")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="JNK" & group=="treatment2")$composite)$conf.int[1:2],
NA,
t.test(subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="NET" & group=="treatment1")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="NET" & group=="treatment2")$composite)$conf.int[1:2],
NA,
t.test(subset(mturk, issue=="IMM" & group=="control")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="IMM" & group=="treatment")$composite)$conf.int[1:2],
NA,
t.test(subset(mturk, issue=="SOC" & group=="control")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="SOC" & group=="treatment")$composite)$conf.int[1:2],
NA,
t.test(subset(mturk, issue=="VAC" & group=="control")$composite)$conf.int[1:2],
t.test(subset(mturk, issue=="VAC" & group=="treatment")$composite)$conf.int[1:2])


##########################################################
# PLOT results
##########################################################

mp <- barplot(
		rbind(mean(subset(mturk, issue=="FOR" & group!="treatment1" & group!="treatment2")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="FOR" & group=="treatment1")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="FOR" & group=="treatment2")$composite, na.rm=TRUE),
			  NA,
			  mean(subset(mturk, issue=="JNK" & group!="treatment1" & group!="treatment2")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="JNK" & group=="treatment1")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="JNK" & group=="treatment2")$composite, na.rm=TRUE),
			  NA,
			  mean(subset(mturk, issue=="NET" & group!="treatment1" & group!="treatment2")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="NET" & group=="treatment1")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="NET" & group=="treatment2")$composite, na.rm=TRUE),
			  NA,
			  mean(subset(mturk, issue=="IMM" & group=="control")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="IMM" & group=="treatment")$composite, na.rm=TRUE),
			  NA,
			  mean(subset(mturk, issue=="SOC" & group=="control")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="SOC" & group=="treatment")$composite, na.rm=TRUE),
			  NA,
			  mean(subset(mturk, issue=="VAC" & group=="control")$composite, na.rm=TRUE),
			  mean(subset(mturk, issue=="VAC" & group=="treatment")$composite, na.rm=TRUE)), 
			  beside=TRUE, ylim=c(-4,6), 
			  col=c("white", rgb(0,0,0,.2), rgb(0,0,0,.5), NA,
			  		"white", rgb(0,0,0,.2), rgb(0,0,0,.5), NA,
			  		"white", rgb(0,0,0,.2), rgb(0,0,0,.5), NA,
			  		"white", rgb(0,0,0,.7), NA,
			  		"white", rgb(0,0,0,.7), NA,
			  		"white", rgb(0,0,0,.7), NA),
			  main="", ylab="Support/Opposition to Policy Proposals", axes=FALSE)

abline(h=0, lty="dotted")		
axis(1, mp, labels=c(NA, "Foreign Lang Instrn", NA, NA, NA, "Junk Food Taxes", NA, NA, NA, "Net Neutrality", NA, NA, NA, "Guest Wkr Prgm", NA, NA, "Soc Sec Reform", NA, NA, "School Vaccines"), line=1.3, lty=0, cex.axis=.8)
axis(2, seq(-4,6, by =1), las=2)
legend("topleft", bty="n", legend = c("Control group", "Treatment: Public evenly divided (Study 1)", "Treatment: Public heavily supports policy (Study 1)", "Treatment: Reference group heavily opposes (Study 2)"), fill = c("white", rgb(0,0,0,.2), rgb(0,0,0,.5), rgb(0,0,0,.7)), cex=.8)


# plot Confidence Intervals
segments(mp, CIs[,1], mp, CIs[,2])
segments(mp-.1, CIs[,1], mp+.1, CIs[,1])
segments(mp-.1, CIs[,2], mp+.1, CIs[,2])

# plot stars
text(mp, CIs[,2], c("", "", "***", "",
					"", "", "", "",
					"", "", "", "",
					"", "", "",
					"", "", "",
					"", "*"), pos=3, offset = 0.1) # plot above
text(mp, CIs[,1], c("", "", "", "",
					"", "", "**", "",
					"", "", "", "",
					"", "***", "",
					"", "**", "",
					"", ""), pos=1) # plot below
